#include "Stdafx.h"
#include "QueryManager.h"

QueryManager::QueryManager () : dbreader(), dbwriter()
{
	// Nothing here
}

QueryManager *QueryManager::get_instance ()
{
	if (instance == nullptr)
		instance = new QueryManager();

	return instance;
}

void QueryManager::LoadFlightNums (int FlightID, int *VectorsNum, int *Dim, int Interval)
{
	try
	{
		dbreader.LoadFlightNums(FlightID, VectorsNum, Dim, Interval);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadFlightNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadFlightNums (int FlightID, int Tact1, int Tact2, int *VectorsNum, int *Dim, int Interval)
{
	try
	{
		dbreader.LoadFlightNums (FlightID, Tact1, Tact2, VectorsNum, Dim, Interval);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadFlightNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadFlight (int FlightID, double *FlightVectors, int VectorsNum, int Dim, int Interval)
{
	try
	{
		dbreader.LoadFlight(FlightID, FlightVectors, VectorsNum, Dim, Interval);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadFlight exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			FlightVectors[i] = 0.0;
		}
	}
}

void QueryManager::LoadFlight (int FlightID, double *FlightVectors, int Tact1, int Tact2, int VectorsNum, int Dim, int Interval)
{
	try
	{
		dbreader.LoadFlight(FlightID, FlightVectors, Tact1, Tact2, VectorsNum, Dim,  Interval);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadFlight exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			FlightVectors[i] = 0.0;
		}
	}
}


void QueryManager::LoadFlightSubsystem(int FlightID, int SubsystemID, double *FlightVectors, int Tact1, int Tact2, int VectorsNum, int Dim, int Interval)
{
	try
	{
		dbreader.LoadFlightSubsystem(FlightID, SubsystemID, FlightVectors, Tact1, Tact2, VectorsNum, Dim,  Interval);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadSubsystem exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			FlightVectors[i] = 0.0;
		}
	}
}

void QueryManager::LoadVectorsNums (int FlightID, int *Tacts, int N, int *VectorsNum, int *Dim)
{
	try
	{
		dbreader.LoadVectorsNums(FlightID, Tacts, N, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadVectorsNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadVectors (int FlightID, double *Vectors, int *Tacts, int N, int VectorsNum, int Dim)
{
	try
	{
		dbreader.LoadVectors(FlightID, Vectors, Tacts, N, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadVectors exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			Vectors[i] = 0.0;
		}
	}
}

void QueryManager::LoadVectorsNums (int PlaneID, double Size, int *VectorsNum, int *Dim)
{
	try
	{
		dbreader.LoadVectorsNums(PlaneID, Size, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadVectorsNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadSubsystemsVectorsNums(int PlaneID, int SubsystemID, double Size, int *VectorsNum, int *Dim)
{
	try
	{
		dbreader.LoadSubsystemsVectorsNums(PlaneID, SubsystemID, Size, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadVectorsNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadVectors (int PlaneID, double *Vectors, double Size, int VectorsNum, int Dim)
{
	try
	{
		dbreader.LoadVectors(PlaneID, Vectors, Size, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadVectors exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			Vectors[i] = 0.0;
		}
	}
}

void QueryManager::LoadSubsystemsVectors (int PlaneID, int SubsystemID, double *Vectors, double Size, int VectorsNum, int Dim)
{
		try
	{
		dbreader.LoadSubsystemsVectors(PlaneID, SubsystemID, Vectors, Size, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadSubsystemsVectors exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			Vectors[i] = 0.0;
		}
	}
}

void QueryManager::LoadClusterVectorsNums (int PlaneID, double Size, int *VectorsNum, int *Dim, char *Date, int Label)
{
	try
	{
		dbreader.LoadClusterVectorsNums(PlaneID, Size, VectorsNum, Dim, Date, Label);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadClusterVectorsNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadClusterVectors (int PlaneID, double *Vectors, double Size, int VectorsNum, int Dim, char *Date, int Label)
{
	try
	{
		dbreader.LoadClusterVectors(PlaneID, Vectors, Size, VectorsNum, Dim, Date, Label);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadClusterVectors exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			Vectors[i] = 0.0;
		}
	}
}

void QueryManager::GetFlightIDsNum (int PlaneID, int *Num)
{
	try
	{
		dbreader.GetFlightIDsNum(PlaneID, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetFlightIDsNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Num = 0;
	}
}

void QueryManager::GetFlightIDs (int PlaneID, double *IDs, int Num)
{
	try
	{
		dbreader.GetFlightIDs(PlaneID, IDs, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetFlightIDs exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Num; ++i)
		{
			IDs[i] = 0.0;
		}
	}
}

void QueryManager::GetClusteredFlightIDsNum (char *Date, int *Num)
{
	try
	{
		dbreader.GetClusteredFlightIDsNum(Date, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetClusteredFlightIDsNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Num = 0;
	}
}

void QueryManager::GetClusteredFlightIDs (char *Date, double *IDs, int Num)
{
	try
	{
		dbreader.GetClusteredFlightIDs(Date, IDs, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetClusteredFlightIDs exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Num; ++i)
		{
			IDs[i] = 0.0;
		}
	}
}

void QueryManager::SaveClusters (int *FlightIDs, int FlightsNum, char *ClusteringType, int ClustersNum, char *Date, char *Comment,
	double **Labels, double *ClusterParams, int ParamsNum)
{
	try
	{
		dbwriter.SaveClusters(FlightIDs, FlightsNum, ClusteringType, ClustersNum, Date, Comment, Labels, ClusterParams, ParamsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::SaveClusters exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
}

void QueryManager::SaveClusters (int *FlightIDs, int FlightsNum, char *ClusteringType, int ClustersNum, char *Date, char *Comment,
	double **Labels, double *ClusterParams, int ParamsNum, double *UsedParametersNames, int UsedParamsNum)
{
	try
	{
		dbwriter.SaveClusters(FlightIDs, FlightsNum, ClusteringType, ClustersNum, Date, Comment, Labels, ClusterParams, ParamsNum,
			UsedParametersNames, UsedParamsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::SaveClusters exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
}

void QueryManager::UpdateClusters (int *FlightIDs, int FlightsNum, int ClustersNum, char *Date, double **Labels, double *ClusterParams, int ParamsNum)
{
	try
	{
		dbwriter.UpdateClusters(FlightIDs, FlightsNum, ClustersNum, Date, Labels, ClusterParams, ParamsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::UpdateClusters exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
}

void QueryManager::UpdateClusters (int *FlightIDs, int FlightsNum, int ClustersNum, char *Date, double **Labels,
	double *ClusterParams, int ParamsNum, double *UsedParametersNames, int UsedParamsNum)
{
	try
	{
		dbwriter.UpdateClusters(FlightIDs, FlightsNum, ClustersNum, Date, Labels, ClusterParams, ParamsNum,
			UsedParametersNames, UsedParamsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::UpdateClusters exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
}

void QueryManager::LoadClusteringNums (int FlightID, int *VectorsNum, int *ParamSize, char *Date)
{
	try
	{
		dbreader.LoadClusteringNums(FlightID, VectorsNum, ParamSize, Date);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadClusteringNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*ParamSize = 0;
	}
}

void QueryManager::LoadClustering (int FlightID, double *Labels, double *Params, int VectorsNum, int ParamSize, char *Date)
{
	try
	{
		dbreader.LoadClustering(FlightID, Labels, Params, VectorsNum, ParamSize, Date);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadCLustering exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < VectorsNum; ++i)
		{
			Labels[i] = 0.0;
		}
		Params[0] = 1.0;
		Params[1] = 2.0;
		Params[2] = 1.0;
		Params[3] = ParamSize - 4.0;
		for (int i = 4; i < ParamSize; ++i)
		{
			Params[i] = 0.0;
		}
	}
}

void QueryManager::LoadClusteringNums (int FlightID, int *VectorsNum, int *ParamSize, int *UsedParamsNum, char *Date)
{
	try
	{
		dbreader.LoadClusteringNums(FlightID, VectorsNum, ParamSize, UsedParamsNum, Date);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadClusteringNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*ParamSize = 0;
	}
}

void QueryManager::LoadClustering (int FlightID, double *Labels, double *Params, double *UsedParametersNames,
	int VectorsNum, int ParamSize, int UsedParamsNum, char *Date)
{
	try
	{
		dbreader.LoadClustering(FlightID, Labels, Params, UsedParametersNames, VectorsNum, ParamSize, UsedParamsNum, Date);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadCLustering exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < VectorsNum; ++i)
		{
			Labels[i] = 0.0;
		}
		Params[0] = 1.0;
		Params[1] = 2.0;
		Params[2] = 1.0;
		Params[3] = ParamSize - 4.0;
		for (int i = 4; i < ParamSize; ++i)
		{
			Params[i] = 0.0;
		}

		UsedParametersNames[0] = 1.0;
		UsedParametersNames[0] = 2.0;
		UsedParametersNames[0] = 3.0;
		UsedParametersNames[0] = UsedParamsNum - 4.0;
		for (int i = 4; i < UsedParamsNum; ++i)
		{
			UsedParametersNames[i] = 0.0;
		}
	}
}

int QueryManager::SaveMetricRealization (char *MetricType, char *Date, double *Params, int ParamsNum, char *Note)
{
	try
	{
		return dbwriter.SaveMetricRealization(MetricType, Date, Params, ParamsNum, Note);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::SaveMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		return 0;
	}
}

int QueryManager::SaveMetricRealization (char *MetricType, char *Date, double *Params, int ParamsNum,
	double *UsedParametersNames, int UsedParamsNum, char *Note)
{
	try
	{
		return dbwriter.SaveMetricRealization(MetricType, Date, Params, ParamsNum, UsedParametersNames, UsedParamsNum, Note);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::SaveMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		return 0;
	}
}

int QueryManager::UpdateMetricRealization (int MetricRealizationID, double *Params, int ParamsNum)
{
	try
	{
		return dbwriter.UpdateMetricRealization(MetricRealizationID, Params, ParamsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::UpdateMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		return 0;
	}
}

int QueryManager::UpdateMetricRealization (int MetricRealizationID, double *Params, int ParamsNum,
	double *UsedParametersNames, int UsedParamsNum)
{
	try
	{
		return dbwriter.UpdateMetricRealization(MetricRealizationID, Params, ParamsNum, UsedParametersNames, UsedParamsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::UpdateMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		return 0;
	}
}

int QueryManager::SaveMetric (int MetricRealizationID, int FlightID, double *Values, char *Type, int ValuesNum)
{
	try
	{
		return dbwriter.SaveMetric (MetricRealizationID, FlightID, Values, Type, ValuesNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::SaveMetric exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		return 0;
	}
}

void QueryManager::LoadMetricRealizationNum (char *MetricType, char *Date, int *RealizationsNum)
{
	try
	{
		dbreader.LoadMetricRealizationNum(MetricType, Date, RealizationsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricRealizationNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*RealizationsNum = 0;
	}
}

void QueryManager::LoadMetricRealizationNum (int ID, int *Size)
{
	try
	{
		dbreader.LoadMetricRealizationNum(ID, Size);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricRealizationNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Size = 0;
	}
}

void QueryManager::LoadMetricRealizationNum (int ID, int *Size, int *UsedSize)
{
	try
	{
		dbreader.LoadMetricRealizationNum(ID, Size, UsedSize);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricRealizationNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Size = 0;
		*UsedSize = 0;
	}
}

void QueryManager::LoadMetricRealization (char *MetricType, char *Date, double *IDs, char *Comments[], int RealizationsNum)
{
	try
	{
		dbreader.LoadMetricRealization(MetricType, Date, IDs, Comments, RealizationsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < RealizationsNum; ++i)
		{
			IDs[i] = 0.0;
			Comments[i][0] = '\0';
		}
	}
}

void QueryManager::LoadMetricRealization (int ID, char *Type, double *Params, char *Comment, int Size)
{
	try
	{
		dbreader.LoadMetricRealization(ID, Type, Params, Comment, Size);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Size; ++i)
		{
			Params[i] = 0.0;
		}
		Comment[0] = '\0';
	}
}

void QueryManager::LoadMetricRealization (int ID, char *Type, double *Params, char *Comment, int Size,
	double *UsedParametersNames, int UsedSize)
{
	try
	{
		dbreader.LoadMetricRealization(ID, Type, Params, Comment, Size, UsedParametersNames, UsedSize);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricRealization exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Size; ++i)
		{
			Params[i] = 0.0;
		}
		Comment[0] = '\0';
	}
}

void QueryManager::GetEvaluatedFlightIDsNum (int ID, int *Num)
{
	try
	{
		dbreader.GetEvaluatedFlightIDsNum(ID, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetEvaluatedFlightIDsNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Num = 0;
	}
}

void QueryManager::GetEvaluatedFlightIDs (int ID, double *IDs, int Num)
{
	try
	{
		dbreader.GetEvaluatedFlightIDs(ID, IDs, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetEvaluatedFlightIDs exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Num; ++i)
		{
			IDs[i] = 0.0;
		}
	}
}

void QueryManager::LoadMetricTypesNum (int ID, int FlightID, int *MetricsNum)
{
	try
	{
		dbreader.LoadMetricTypesNum(ID, FlightID, MetricsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricTypesNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*MetricsNum = 0;
	}
}

void QueryManager::LoadMetricTypes (int ID, int FlightID, double *TypeIDs, char *Types[], int MetricsNum)
{
	try
	{
		dbreader.LoadMetricTypes(ID, FlightID, TypeIDs, Types, MetricsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricTypes exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < MetricsNum; ++i)
		{
			TypeIDs[i] = 0.0;
			Types[i][0] = '\0';
		}
	}
}

void QueryManager::LoadMetricNum (int ID, int FlightID, int TypeID, int *Size)
{
	try
	{
		dbreader.LoadMetricNum(ID, FlightID, TypeID, Size);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetricNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Size = 0;
	}
}

void QueryManager::LoadMetric (int ID, int FlightID, int TypeID, double *Values, int Size)
{
	try
	{
		dbreader.LoadMetric(ID, FlightID, TypeID, Values, Size);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadMetric exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Size; ++i)
		{
			Values[i] = 0.0;
		}
	}
}

void QueryManager::LoadProblems (int FlightID, double *Problems, int Size)
{
	try
	{
		dbreader.LoadProblems(FlightID, Problems, Size);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadProblems exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Size; ++i)
		{
			Problems[i] = 1;
		}
	}
}

void QueryManager::LoadFlightVectorsNums (int FlightID, double Size, int *VectorsNum, int *Dim)
{
	try
	{
		dbreader.LoadFlightVectorsNums(FlightID, Size, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadFlightVectorsNums exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*VectorsNum = 0;
		*Dim = 0;
	}
}

void QueryManager::LoadFlightVectors (int FlightID, double *Vectors, double Size, int VectorsNum, int Dim)
{
	try
	{
		dbreader.LoadFlightVectors(FlightID, Vectors, Size, VectorsNum, Dim);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadFlightVectors exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < (VectorsNum * Dim); ++i)
		{
			Vectors[i] = 0.0;
		}
	}
}

int QueryManager::GetClustersNum (char *Date)
{
	try
	{
		return dbreader.GetClustersNum(Date);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetClustersNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->StackTrace);
		Log->WriteLine(Exception->Source);
		Log->Flush();

		Log->Close();

		return 0;
	}
}

void QueryManager::GetClusteringRunDate (int MetricRealizationID, char *Date)
{
	try
	{
		return dbreader.GetClusteringRunDate(MetricRealizationID, Date);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetClusteringRunDate exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->StackTrace);
		Log->WriteLine(Exception->Source);
		Log->Flush();

		Log->Close();

		strcpy(Date, "1800-12-01 23:59:59");
	}
}

void QueryManager::GetParameterIDsNum (int PlaneID, int *Num)
{
	try
	{
		dbreader.GetParameterIDsNum(PlaneID, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetParameterIDsNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Num = 0;
	}
}

void QueryManager::GetParameterIDs (int PlaneID, double *IDs, int Num)
{
	try
	{
		dbreader.GetParameterIDs(PlaneID, IDs, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::GetParameterIDs exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Num; ++i)
		{
			IDs[i] = 0.0;
		}
	}
}

void QueryManager::LoadParameterNum (int FlightID, int ParameterID, int *Num)
{
	try
	{
		dbreader.LoadParameterNum(FlightID, ParameterID, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadParameterNum exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		*Num = 0;
	}
}

void QueryManager::LoadParameter (int FlightID, double *Values, int ParameterID, int Num)
{
	try
	{
		dbreader.LoadParameter(FlightID, Values, ParameterID, Num);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBReader::LoadParameter exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();

		for (int i = 0; i < Num; ++i)
		{
			Values[i] = 0.0;
		}
	}
}

int QueryManager::InsertNewFlight(char* FlightName, char* PlaneName, char* StartDateTime, char* StopDateTime, int* PlaneId)
{
	int FlightId = -1;
	try
	{
		FlightId = dbwriter.InsertNewFlight(FlightName, PlaneName, StartDateTime, StopDateTime, PlaneId);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::InsertNewFlight exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
	return FlightId;
}

void QueryManager::RegisterVectors(int ProcessedFlightID, int Freq, int TactsNum)
{
	try
	{
		dbwriter.RegisterVectors(ProcessedFlightID, Freq, TactsNum);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::RegisterVectors exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
}

void QueryManager::SaveXMLBlank(int MetricRealizationID, int FlightID, char *cXMLPath)
{
	try
	{
		dbwriter.SaveXMLBlank(MetricRealizationID, FlightID, cXMLPath);
	}
	catch(System::Exception ^Exception)
	{
		System::IO::StreamWriter ^Log = gcnew System::IO::StreamWriter("exceptionlog.txt", true);

		Log->WriteLine(System::DateTime::Now + " DBWriter::SaveXMLBlank exception.");
		Log->WriteLine(Exception->Message);
		Log->WriteLine(Exception->Source);
		Log->WriteLine(Exception->StackTrace);
		Log->Flush();

		Log->Close();
	}
}